package com.itheima.mp.service.serviceImp;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.dto.pageDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.enumeration.UserStatus;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImp extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    UserMapper userMapper;

    @Override
    public void deductBalance(Long id, Integer money) {
        //1.查询用户
        User user = getById(id);
        //2.看状态是否正常
        if (user.getStatus() == UserStatus.FROZE) {
            throw new RuntimeException("用户状态异常!");
        }
        //3.余额是否充足
        if (user.getBalance() < money) {
            throw new RuntimeException("余额不足!");
        }
        //4.扣费：并且如果余额是0设置状态为异常
        int remainBalance = user.getBalance() - money;
        lambdaUpdate().set(User::getBalance, remainBalance).set(remainBalance == 0, User::getStatus, 2).update();
    }

    /**
     * 查询用户和收货地址
     *
     * @param id
     * @return
     */
//    @Override
//    public UserVO queryUserAndAddressById(Long id) {
//        //1.查询用户
//        User user = getById(id);
//        //2.判断是否异常
//        if (user==null||user.getStatus()==UserStatus.FROZE){
//            throw new RuntimeException("用户状态异常！");
//        }
//        //3.查询地址
//        List<Address> addresses = Db.lambdaQuery(Address.class).eq(Address::getUserId, id).list();
//        //3.封装返回
//        List<AddressVO> addressVOS = BeanUtil.copyToList(addresses, AddressVO.class);
//        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
//        userVO.setAddressVOS(addressVOS);
//        return userVO;
//    }

    @Override
    public UserVO queryUserAndAddressById(Long id) {
        return null;
    }


    /**
     * 根据用户条件分页查询
     * @param userQuery
     * @return
     */
    @Override
    public PageDTO<UserVO> userPage(UserQuery userQuery) {
        //1.准备条件
        Page<User> page = userQuery.set();
        //2.分页查询
        Page<User> pages = lambdaQuery().
                eq(userQuery.getStatus() != null, User::getStatus, userQuery.getStatus())
                .like(userQuery.getUserName() != null, User::getUsername, userQuery.getUserName())
                .page(page);
        //3.封装成dto
        PageDTO<UserVO> pageDTO = userQuery.voToDTO(pages,UserVO.class);
        //4.返回
        return pageDTO;
    }
}
